package com.zjmcollege.hotpot.search.service.impl;

import com.zjmcollege.hotpot.entity.Article;
import com.zjmcollege.hotpot.search.model.SearchResultModel;
import com.zjmcollege.hotpot.search.repository.SearchRepository;
import com.zjmcollege.hotpot.service.ArticleService;
import com.zjmcollege.hotpot.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-11-08 10:12
 */
@Service
public class SearchServiceImpl implements SearchService {
//    @Autowired
//    private ArticleService articleService;

    @Autowired
    private SearchRepository searchRepository;

    @Override
    public Page<SearchResultModel> search(String wd, int page, int pageSize) {
        Sort sort = Sort.by(Sort.Direction.DESC, "support");
        Pageable pageable = PageRequest.of(--page, pageSize, sort);  //分页信息
        Page<SearchResultModel> pages = searchRepository.findByKey(wd + "*", pageable);
//        Page<SearchResultModel> searchPages = null;
//        List<SearchResultModel> searchResultModels = new ArrayList<>();
//
//        if (pages.getTotalElements() > 0) {
//            List<Article> articles = pages.getContent();
//            articles.forEach(article -> {
//                SearchResultModel searchResultModel = new SearchResultModel();
//                searchResultModel.setAuthor(article.getAuthor());
//                searchResultModel.setTitle(article.getTitle());
//                searchResultModel.setBrief(article.getBrief() + "...");
//
//                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                searchResultModel.setPostedTime(format.format(article.getCreatedTime()));
//                searchResultModel.setUrl("/blog/" + article.getId());
//
//                searchResultModels.add(searchResultModel);
//            });
//        }
//
//        searchPages = new PageImpl<SearchResultModel>(searchResultModels, PageRequest.of(
//                page, pageSize, Sort.by(Sort.Direction.ASC, "id")),
//                pages.getTotalElements());
        return pages;
    }

    @Override
    public SearchResultModel updateSupport(String id) {
        Optional<SearchResultModel> searchResult = searchRepository.findById(id);
        SearchResultModel result = searchResult.get();
        if (result != null) {
            // 增加反对数量
            result.setSupport(result.getSupport() + 1);
            searchRepository.save(result);
        }
        return result;
    }

    @Override
    public SearchResultModel updateOppose(String id) {
        Optional<SearchResultModel> searchResult = searchRepository.findById(id);
        SearchResultModel result = searchResult.get();
        if (result != null) {
            // 增加反对数量
            result.setOppose(result.getOppose() + 1);
            searchRepository.save(result);
        }
        return result;
    }
}
